/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.utils;

import org.testis.data.AtofModuleConstants;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

/**
 *
 * @author nurhat
 */
public class UserSettingsManager {

    boolean isLoaded = false;
    PropertyFileProvider propFile;
    static UserSettingsManager _manager;

    private UserSettingsManager() {
        propFile = new PropertyFileProvider();
        isLoaded = propFile.loadPropertiesFile(AtofModuleConstants.ATOF_USER_TEMP_DIRECTORY
                + "/"
                + "userconfiguration.props",
                false);
        System.out.println("Current user Home Directory:" + AtofModuleConstants.ATOF_USER_TEMP_DIRECTORY);
    }

    public static synchronized UserSettingsManager getInstance() {
        if (_manager == null) {
            _manager = new UserSettingsManager();
        }
        return _manager;
    }

    public boolean isRememberMe() {
        String value = "true";
        if (isLoaded) {
            value = propFile.getProperty("REMEMBER_ME");
        }
        return Boolean.parseBoolean(value);
    }

    public void setRememberMe(Boolean value) {
        propFile.putProperty("REMEMBER_ME", value.toString(), "");

    }

    public String getSelectedDynamicMaps() {
        String value = "";
        if (isLoaded) {
            value = propFile.getProperty("SELECT_DYNAMIC_MAP");
        }
        return value;
    }

    public void setSelectedDynamicMaps(String value) {
        propFile.putProperty("SELECT_DYNAMIC_MAP", value, "");
    }

    public String getPassword() {
        String value = "";
        if (isLoaded) {
            value = propFile.getProperty("PASSWORD");
        }
        return value;
    }

    public void setPassword(String value) {
        propFile.putProperty("PASSWORD", value, "");
    }

    public String getUserName() {
        String value = "";
        if (isLoaded) {
            value = propFile.getProperty("USER_NAME");
        }
        return value;
    }

    public void setUserName(String value) {
        propFile.putProperty("USER_NAME", value, "");
    }

    public String getSelectedSuites() {
        String value = "";
        if (isLoaded) {
            value = propFile.getProperty("SELECT_SUITE");
        }
        return value;
    }

    public void setSelectedSuites(String value) {
        propFile.putProperty("SELECT_SUITE", value, "");
    }

    public String getUpdateUserName() {
        String value = "";
        if (isLoaded) {
            value = propFile.getProperty("UPDATE_USERNAME");
        }
        return value;
    }

    public void setUpdateUserName(String value) {
        propFile.putProperty("UPDATE_USERNAME", value, "");
    }

    public String getUpdatePassword() {
        String value = "";
        if (isLoaded) {
            value = propFile.getProperty("UPDATE_PASSWORD");
        }
        return value;
    }

    public void setUpdatePassword(String value) {
        propFile.putProperty("UPDATE_PASSWORD", value, "");
    }

    public String getUpdateUrl() {
        String value = "";
        if (isLoaded) {
            value = propFile.getProperty("UPDATE_URL");
        }
        return value;
    }

    public void setUpdateUrl(String value) {
        propFile.putProperty("UPDATE_URL", value, "");
    }

    public boolean isAutoUpdate() {
        String value = "false";
        if (isLoaded) {
            value = propFile.getProperty("AUTO_UPDATE_ACTIVE");
        }
        return Boolean.parseBoolean(value);
    }

    public void setAutoUpdate(Boolean value) {
        propFile.putProperty("AUTO_UPDATE_ACTIVE", value.toString(), "");

    }
    ////

    public String getDbUserName() {
        String value = "root";
        if (isLoaded) {
            value = propFile.getProperty("DATABASE_USERNAME");
        }
        return value;
    }

    public void setDbUserName(String value) {
        propFile.putProperty("DATABASE_USERNAME", value, "");
    }

    public String getDbPassword() {
        String value = "12345";
        if (isLoaded) {
            value = propFile.getProperty("DATABASE_PASSWORD");
        }
        return value;
    }

    public void setDbPassword(String value) {
        propFile.putProperty("DATABASE_PASSWORD", value, "");
    }

    public String getDbUrl() {
        String value = "localhost";
        if (isLoaded) {
            value = propFile.getProperty("DATABASE_URL");
        }
        return value;
    }

    public void setDbUrl(String value) {
        propFile.putProperty("DATABASE_URL", value, "");
    }

    public String getDbCatalogName() {
        String value = "atof";
        if (isLoaded) {
            value = propFile.getProperty("DATABASE_CATALOGNAME");
        }
        return value;
    }

    public void setDbCatalogName(String value) {
        propFile.putProperty("DATABASE_CATALOGNAME", value, "");
    }

    public String getActiveMqUrl() {
        String value = "localhost";
        if (isLoaded) {
            value = propFile.getProperty("ACTIVEMQ_URL");
        }
        return value;
    }

    public void setActiveMqUrl(String value) {
        propFile.putProperty("ACTIVEMQ_URL", value, "");
    }

    public List<String> getTestPlanConfigs() {
        List<String> value = new ArrayList<String>();
        Collections.sort(value);
        if (isLoaded) {
            Enumeration<String> propertyNames = propFile.getPropertyNames();
            while (propertyNames.hasMoreElements()) {
                String element = propertyNames.nextElement();
                if (element.startsWith("TPCONFIG_")) {
                    value.add(element.replace("TPCONFIG_",""));
                }
            }
        }

        return value;
    }

    public String[] getTestPlanConfigByName(String name) {
        String configValue = propFile.getProperty("TPCONFIG_"+name);
        if (configValue == null) {
            return null;
        }
        return configValue.split("|");
    }

    public void setTestPlanConfigs(String name, String value) {
        propFile.putProperty("TPCONFIG_" + name, value, "");
    }

    public void deleteTestPlanConfigs(String name) {
        propFile.removeProperty(name);
    }

    public boolean isUpdateRequired() {
        String value = "false";
        if (isLoaded) {
            value = propFile.getProperty("APPLY_UPDATE_REQUIRED");
        }
        return Boolean.parseBoolean(value);
    }

    public void setUpdateRequired(Boolean value) {
        propFile.putProperty("APPLY_UPDATE_REQUIRED", value.toString(), "");

    }

    public boolean isDontshowAtStartup() {
        String value = "true";
        if (isLoaded) {
            value = propFile.getProperty("DONT_SHOW_AT_STARTUP");
        }
        return Boolean.parseBoolean(value);
    }

    public void setDontshowAtStartup(Boolean value) {
        propFile.putProperty("DONT_SHOW_AT_STARTUP", value.toString(), "");

    }
}
